package com.xjh.him.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.adapter.SmartViewHolder;
import com.xjh.him.R;
import com.xjh.him.core.WeChatPresenter;
import com.xjh.him.utils.Utils;
import com.ypx.imagepicker.ImagePicker;
import com.ypx.imagepicker.bean.ImageItem;
import com.ypx.imagepicker.data.OnImagePickCompleteListener;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TrendPublishAdapter extends RecyclerView.Adapter<SmartViewHolder>{
    public static final int ITEM_TYPE_ADD = 1;
    public static final int ITEM_TYPE_PIC = 2;

    public static final int MAX_ITEM_NUM = 3;

    private Activity mActivity;
    private List<String> pathList;

    public TrendPublishAdapter(Activity activity) {
        mActivity = activity;
        pathList = new ArrayList<>();
    }

    @NonNull
    @Override
    public SmartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mActivity)
                .inflate(R.layout.adapter_trend_publish_item, parent, false);
        return new SmartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SmartViewHolder holder, int position) {
        ImageView selectImage = holder.findViewById(R.id.iv_select_pic);
        ImageView deleteImage = holder.findViewById(R.id.iv_delete);
        if (pathList.size() != MAX_ITEM_NUM && position == pathList.size()) {
            //view会复用，必须重新设置，否则可能会出现删除的图片
            selectImage.setImageResource(R.drawable.ic_add_image);
            deleteImage.setVisibility(View.GONE);
            setOnItemAddClickListener(selectImage);
        }else {
            deleteImage.setVisibility(View.VISIBLE);
            String path = pathList.get(position);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.color_f4);
            Glide.with(mActivity)
                    .load(path)
                    .apply(options)
                    .into(selectImage);
            setOnItemClickListener(selectImage, position);
            setOnItemDeleteClickListener(deleteImage, position);
        }
    }

    @SuppressLint("NewApi")
    private void setOnItemAddClickListener(ImageView imageView) {
        imageView.setOnClickListener(v -> {
            ArrayList<String> lastPathList = (ArrayList<String>) pathList.stream()
                    .filter(item -> !"".equals(item))
                    .collect(Collectors.toList());
            Utils.getWeChatPickerBuilder()
                .setMaxCount(MAX_ITEM_NUM)
                .setLastImageList(lastPathList)
                .pick(mActivity, new OnImagePickCompleteListener() {
                    @Override
                    public void onImagePickComplete(ArrayList<ImageItem> items) {
                        pathList = items.stream()
                                .map(ImageItem::getPath)
                                .collect(Collectors.toList());
                        notifyDataSetChanged();
                    }
                });
        });
    }

    private void setOnItemDeleteClickListener(ImageView imageView, int position) {
        imageView.setOnClickListener(v -> {
            pathList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, pathList.size());
        });
    }

    private void setOnItemClickListener(ImageView imageView, int position) {
        imageView.setOnClickListener(v -> {
            ImagePicker.preview(mActivity, new WeChatPresenter(), new ArrayList<>(pathList), position, new OnImagePickCompleteListener() {
                @Override
                public void onImagePickComplete(ArrayList<ImageItem> items) {

                }
            });
        });
    }

    @Override
    public int getItemCount() {
        return pathList.size() < MAX_ITEM_NUM ? pathList.size() + 1 : MAX_ITEM_NUM;
    }

    public List<String> getPathList() {
        return pathList;
    }
}
